import db from "../models/index.js";
import {Op} from "sequelize";
import {getNow} from "../utils/utils.js";

let company = db.company;
let companyDao = {};

// 创建公司
companyDao.createCompany = async (company_name, creator_id, t) => {
  let create_time = getNow();
  return company.create({company_name, creator_id, create_time}, {transaction: t});
};

// 注销公司/逻辑删除
companyDao.deleteCompany = async (id, t) => {
  return company.update({is_deleted: 1}, {where: {id}, transaction: t});
};

// 修改公司信息
companyDao.updateCompanyName = async (id, company_name, t) => {
  return company.update({company_name}, {where: {id}, transaction: t});
};

// 获取公司信息
companyDao.getCompany = async (id) => {
  return company.findOne({where: {id}, raw: true});
};

// 获取公司信息
companyDao.getCompanyName = async (id) => {
  return company.findOne({
    attributes: ['company_name'],
    where: {id},
    raw: true
  });
};

// 判断公司名称
companyDao.isCompanyNameExist = async (company_name) => {
  return company.findOne({where: {company_name}, raw: true});
};

export default companyDao;
